package com.xbongbong.saas.help.workflow;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.product.pojo.ProductSavePojo;
import com.xbongbong.saas.domain.entity.InstockProductEntity;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import com.xbongbong.saas.domain.entity.TransferProductEntity;
import com.xbongbong.saas.enums.InstockTypeEnum;
import com.xbongbong.saas.help.SaasProductHelp;
import com.xbongbong.saas.model.OutstockProductModel;
import com.xbongbong.saas.model.TransferProductModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 *
 */
@Component
@Slf4j
public class TransferHelp {

    @Resource
    private SaasProductHelp saasProductHelp;
    @Resource
    private OutstockProductModel outstockProductModel;
    @Resource
    private TransferProductModel transferProductModel;

    public boolean judgeTransferAllInBound(Long instockId, Long outstockId, Long refId, String corpid, List<ProductSavePojo> productSavePojoList) throws XbbException {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("outWarehouseId", outstockId);
        param.put("del", 0);
        // 拿到合同的所有产品
        List<OutstockProductEntity> outstockProductEntityList = outstockProductModel.findEntitys(param);
        Map<Long,Double> outstockProductMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (OutstockProductEntity entity : outstockProductEntityList){
            Double num = outstockProductMap.getOrDefault(entity.getProductId(),0D);
            Double productNum = entity.getProductNum() == null ? 0D : Math.abs(entity.getProductNum());
            num = Arith.add(num,productNum);
            outstockProductMap.put(entity.getProductId(),num);
        }
        return saasProductHelp.judgeAllInBound(outstockProductMap,refId, InstockTypeEnum.TRANSFER_INSTOCK.getCode(),corpid,productSavePojoList,instockId);
    }


    public void updateTransferInstockNum(List<Long> refIds, String corpid, List<ProductSavePojo> productSavePojoList, List<InstockProductEntity> instockProductEntities) throws XbbException {
        try {
            HashSet<Long> transferIds = new HashSet<>();
            for (Long id : refIds) {
                transferIds.add(id);
            }
            List<TransferProductEntity> updateList = new ArrayList<>();
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("transferIdIn", transferIds);
            param.put("del", 0);
            // 拿到所有产品
            List<TransferProductEntity> transferProductEntityList = transferProductModel.findEntitys(param);
            Map<Object, Double> transferInstockNumMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<Object, Long> transferOutstockMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.clear();
            param.put("corpid", corpid);
            param.put("del", 0);
            param.put("refIdIn", transferIds);
            List<OutstockProductEntity> entitys = outstockProductModel.findEntitys(param);
            for (OutstockProductEntity entity : entitys) {
                transferOutstockMap.put(entity.getId(), entity.getRefProductId());
            }


            if (Objects.isNull(instockProductEntities)) {
                for (ProductSavePojo productSavePojo : productSavePojoList) {
                    if (transferOutstockMap.containsKey(productSavePojo.getRefProductId())) {
                        Long id = transferOutstockMap.get(productSavePojo.getRefProductId());
                        Double num = transferInstockNumMap.getOrDefault(id, 0D);
                        num = Arith.add(productSavePojo.getNum(),num);
                        transferInstockNumMap.put(id, num);
                    }
                }
                for (TransferProductEntity entity : transferProductEntityList) {
                    if (transferInstockNumMap.containsKey(entity.getId())) {
                        entity.setInstockNum(transferInstockNumMap.getOrDefault(entity.getId(), 0D));
                        entity.setUpdateTime(DateTimeUtil.getInt());
                        updateList.add(entity);
                    }
                }
            } else {
                //删除逻辑
                for (InstockProductEntity entity : instockProductEntities) {
                    if (transferOutstockMap.containsKey(entity.getRefProductId())) {
                        Long id = transferOutstockMap.get(entity.getRefProductId());
                        Double num = transferInstockNumMap.getOrDefault(id, 0D);
                        num = Arith.add(entity.getProductNum(), num);
                        transferInstockNumMap.put(id, num);
                    }
                }

                for (TransferProductEntity entity : transferProductEntityList) {
                    if (transferInstockNumMap.containsKey(entity.getId())) {
                        entity.setInstockNum(transferInstockNumMap.getOrDefault(entity.getId(), 0D) * (-1));
                        entity.setUpdateTime(DateTimeUtil.getInt());
                        updateList.add(entity);
                    }
                }

            }
            if (!updateList.isEmpty()) {
                transferProductModel.updateInstock(updateList,corpid);
            }
        } catch (Exception e) {
            log.error("transferServiceImpl.updateTransferInstockNum 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());        }
    }



    public void updateTransferOutstockNum(List<Long> refIds, String corpid, List<ProductSavePojo> productSavePojoList,Boolean isSave) throws XbbException {
        try {
            List<TransferProductEntity> updateList = new ArrayList<>();
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("transferIdIn", refIds);
            param.put("del", 0);
            // 拿到所有产品
            List<TransferProductEntity> transferProductEntityList = transferProductModel.findEntitys(param);
            if (isSave) {
                Map<Long, Double> transferOutstockNumMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                for (ProductSavePojo productSavePojo : productSavePojoList) {
                    Double num = transferOutstockNumMap.getOrDefault(productSavePojo.getRefProductId(), 0D);
                    num = Arith.add(productSavePojo.getNum(),num);
                    transferOutstockNumMap.put(productSavePojo.getRefProductId(), num);
                }
                for (TransferProductEntity entity : transferProductEntityList) {
                    if (transferOutstockNumMap.containsKey(entity.getId())) {
                        entity.setOutstockNum(transferOutstockNumMap.get(entity.getId()));
                        entity.setUpdateTime(DateTimeUtil.getInt());
                        updateList.add(entity);
                    }
                }
            } else {
                for (TransferProductEntity entity : transferProductEntityList) {
                    entity.setOutstockNum(0D);
                    entity.setUpdateTime(DateTimeUtil.getInt());
                    updateList.add(entity);
                }
            }
            if (!updateList.isEmpty()) {
                transferProductModel.updateBatch(updateList,corpid);
            }
        } catch (Exception e) {
            log.error("transferServiceImpl.updateTransferOutstockNum 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());        }
    }



}
